<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药片名称" prop="drugName">
        <el-input
          v-model="queryParams.drugName"
          placeholder="请输入药片名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="药片关键字" prop="drugKeyword">
        <el-input
          v-model="queryParams.drugKeyword"
          placeholder="请输入药片关键字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="药品类型" prop="drugType">
        <el-select v-model="queryParams.drugType" placeholder="请选择药品类型" clearable>
          <el-option
            v-for="dict in drug_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处方类型" prop="drugPrescription">
        <el-select v-model="queryParams.drugPrescription" placeholder="请选择处方类型" clearable>
          <el-option
            v-for="dict in drug_prescription"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产厂家" prop="drugPrescription">
        <el-select v-model="queryParams.factoryId" placeholder="请选择厂家" style="width: 200px" clearable>
          <el-option
            v-for="dict in factoryList"
            :key="dict"
            :label="dict.factoryName"
            :value="dict.factoryId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['medicine:drug:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['medicine:drug:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['medicine:drug:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['medicine:drug:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="药品ID" align="center" prop="drugId" />
      <el-table-column label="药片名称" align="center" prop="drugName" />
      <el-table-column label="药品编码" align="center" prop="drugCode" />
      <el-table-column label="生产厂家" align="center" prop="hisMedicineFactory.factoryName">
      </el-table-column>
      <el-table-column label="药品类型" align="center" prop="drugType">
        <template #default="scope">
          <dict-tag :options="drug_type" :value="scope.row.drugType"/>
        </template>
      </el-table-column>
      <el-table-column label="处方类型" align="center" prop="drugPrescription">
        <template #default="scope">
          <dict-tag :options="drug_prescription" :value="scope.row.drugPrescription"/>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="drugUnit">
        <template #default="scope">
          <dict-tag :options="drug_unit" :value="scope.row.drugUnit"/>
        </template>
      </el-table-column>
      <el-table-column label="销售价格" align="center" prop="drugPrice" />
      <el-table-column label="库存数量" align="center" prop="drugNum" />
      <el-table-column label="预警值" align="center" prop="drugWarn" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['medicine:drug:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['medicine:drug:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="drugRef" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="药片名称" prop="drugName">
          <el-input v-model="form.drugName" placeholder="请输入药片名称" />
        </el-form-item>
        <el-form-item label="药品编码" prop="drugCode">
          <el-input v-model="form.drugCode" placeholder="请输入药品编码" />
        </el-form-item>
        <el-form-item label="药片关键字" prop="drugKeyword">
          <el-input v-model="form.drugKeyword" placeholder="请输入药片关键字" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="factoryId">
          <el-select v-model="form.factoryId" placeholder="请选择生产厂家">
            <el-option
              v-for="dict in factoryList"
              :key="dict.value"
              :label="dict.factoryName"
              :value="dict.factoryId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品类型" prop="drugType">
          <el-select v-model="form.drugType" placeholder="请选择药品类型">
            <el-option
              v-for="dict in drug_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处方类型" prop="drugPrescription">
          <el-select v-model="form.drugPrescription" placeholder="请选择处方类型">
            <el-option
              v-for="dict in drug_prescription"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="drugUnit">
          <el-select v-model="form.drugUnit" placeholder="请选择单位">
            <el-option
              v-for="dict in drug_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="销售价格" prop="drugPrice">
          <el-input-number v-model="form.drugPrice" :precision="2" :step="0.1" :min="0"  placeholder="0.00" size="small"/>
        </el-form-item>
        <el-form-item label="库存数量" prop="drugNum">
          <el-input-number v-model="form.drugNum" placeholder="0"  :min="0" size="small"/>
        </el-form-item>
        <el-form-item label="预警值" prop="drugWarn">
          <el-input-number v-model="form.drugWarn" placeholder="0" :min="0" size="small"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Drug">
import { listDrug, getDrug, delDrug, addDrug, updateDrug } from "@/api/his/drug";
import { listAllFactory } from "@/api/his/factory";
const { proxy } = getCurrentInstance();
const { drug_type, sys_normal_disable, drug_prescription,drug_unit } = proxy.useDict('drug_type', 'sys_normal_disable', 'drug_prescription','drug_unit');

const drugList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const factoryList=ref([])
//加载工厂信息
listAllFactory().then(res=>{
  factoryList.value=res.rows;
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    drugName: null,

    drugKeyword: null,
    factoryId: null,
    drugType: null,
    drugPrescription: null,
    status: null,
  },
  rules: {
    drugCode: [
      { required: true, message: "药品编码不能为空", trigger: "blur" }
    ],
    drugKeyword: [
      { required: true, message: "药片关键字不能为空", trigger: "blur" }
    ],
    factoryId: [
      { required: true, message: "生产厂家ID不能为空", trigger: "blur" }
    ],
    drugType: [
      { required: true, message: "药品类型不能为空", trigger: "change" }
    ],
    drugPrescription: [
      { required: true, message: "处方类型不能为空", trigger: "change" }
    ],
    drugUnit: [
      { required: true, message: "单位不能为空", trigger: "blur" }
    ],
    drugNum: [
      { required: true, message: "库存数量不能为空", trigger: "blur" }
    ],
    drugWarn: [
      { required: true, message: "预警值不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询药品信息列表 */
function getList() {
  loading.value = true;
  listDrug(queryParams.value).then(response => {
    drugList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    drugId: null,
    drugName: null,
    drugCode: null,
    drugKeyword: null,
    factoryId: null,
    drugType: null,
    drugPrescription: null,
    drugUnit: null,
    drugPrice: null,
    drugNum: null,
    drugWarn: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  proxy.resetForm("drugRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.drugId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加药品信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _drugId = row.drugId || ids.value
  getDrug(_drugId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改药品信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["drugRef"].validate(valid => {
    if (valid) {
      if (form.value.drugId != null) {
        updateDrug(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDrug(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _drugIds = row.drugId || ids.value;
  proxy.$modal.confirm('是否确认删除药品信息编号为"' + _drugIds + '"的数据项？').then(function() {
    return delDrug(_drugIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('medicine/drug/export', {
    ...queryParams.value
  }, `drug_${new Date().getTime()}.xlsx`)
}

getList();
</script>
